/*
 * @Author: NJ_1098 xiabaoxin
 * @Date: 2025-02-13 17:14:48
 * @LastEditors: NJ_1098 xiabaoxin
 * @LastEditTime: 2025-02-19 17:15:09
 * @Description: 账单控制层
 */
const moment = require("moment");
const { Op } = require("sequelize");
const BillModel = require("../model/bill.model.js");
const { UUID_LENGTH, UUID_RADIX } = require("../config/config.default");
const CommonUtils = require("../utils/common.js");
const { error } = require("../log/log.js");

class BillService {
  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 新增账单
   * @param {*} name
   * @param {*} zodiac
   * @return {*}
   */
  static async add({ name, zodiac, uid }) {
    try {
      const lastOpt = {};
      name && Object.assign(lastOpt, { name });
      zodiac && Object.assign(lastOpt, { zodiac });
      uid && Object.assign(lastOpt, { uid });
      const bid = CommonUtils.UUID(UUID_LENGTH, UUID_RADIX);
      Object.assign(lastOpt, { bid });
      const res = await BillModel.create(lastOpt);
      return res.dataValues;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 删除账单
   * @param {*} bids
   * @return {*}
   */
  static async delete(bids) {
    try {
      const res = await BillModel.destroy({
        where: {
          bid: {
            [Op.in]: bids,
          },
        },
      });
      let msg = "";
      if (res > 0) {
        msg = `成功删除${res}条数据`;
      } else {
        msg = "未找到删除数据";
      }
      return msg;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 账单列表
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} name
   * @param {*} zodiac
   * @param {*} dateS
   * @param {*} dateE
   * @param {*} uid
   * @return {*}
   */
  static async list({ pageNo, pageSize, name, zodiac, dateS, dateE }, uid) {
    try {
      let where = {};
      zodiac && Object.assign(where, { zodiac });
      name && Object.assign(where, { name: { [Op.like]: `%${name}%` } });
      dateS &&
        Object.assign(where, {
          create_time: {
            [Op.between]: [`${dateS} 00:00:00`, `${dateE} 23:59:59`],
          },
        });
      Object.assign(where, {
        [Op.or]: [{ uid }, { actors: { [Op.like]: `%${uid}%` } }],
      });

      console.log(where);
      const res = await BillModel.findAndCountAll({
        attributes: [
          "bid",
          "name",
          "sum_income",
          "sum_expenses",
          "zodiac",
          "create_time",
        ],
        where,
        offset: (pageNo - 1) * pageSize,
        limit: pageSize,
      });
      res.rows.forEach((item) => {
        item.dataValues.create_time = moment(
          item.dataValues.create_time
        ).format("YYYY-MM-DD HH:mm:ss");
      });
      return res;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 获取账单详情
   * @param {*} bid
   * @return {*}
   */
  static async detail(bid) {
    try {
      const res = await BillModel.findOne({
        attributes: [
          "bid",
          "name",
          "uid",
          "name",
          "sum_income",
          "sum_expenses",
          "zodiac",
          "actors",
          "create_time",
        ],
        where: { bid },
      });
      if (res) {
        res.dataValues.create_time = moment(res.dataValues.create_time).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      }
      return res ? res.dataValues : null;
    } catch (err) {
      throw new Error(err);
    }
  }

  /**
   * @author: NJ_1098 xiabaoxin
   * @description: 更新账单
   * @param {*} bid
   * @param {*} name
   * @param {*} sum_income
   * @param {*} sum_expenses
   * @param {*} zodiac
   * @param {*} actors
   * @return {*}
   */
  static async update(bid, { name, sum_income, sum_expenses, zodiac, actors }) {
    try {
      const updateData = {};
      name && Object.assign(updateData, { name });
      sum_income && Object.assign(updateData, { sum_income });
      sum_expenses && Object.assign(updateData, { sum_expenses });
      zodiac && Object.assign(updateData, { zodiac });
      actors && Object.assign(updateData, { actors });
      const [res] = await BillModel.update(updateData, {
        where: { bid },
      });
      return res;
    } catch (err) {
      throw new Error(err);
    }
  }
}

module.exports = BillService;
